#include <iostream>
#include <string>
#include <stdexcept>
#include <cctype>

class Parser {
public:
    Parser(const std::string& expr) : expression(expr), pos(0) {}

    double parse() {
        return parseExpression();
    }

private:
    std::string expression;
    size_t pos;

    void skipWhitespace() {
        while (pos < expression.size() && std::isspace(expression[pos])) {
            ++pos;
        }
    }

    double parseExpression() {
        double result = parseTerm();
        skipWhitespace();
        while (pos < expression.size()) {
            char op = expression[pos];
            if (op == '+' || op == '-') {
                ++pos;
                double term = parseTerm();
                if (op == '+') {
                    result += term;
                } else {
                    result -= term;
                }
                skipWhitespace();
            } else {
                break;
            }
        }
        return result;
    }

    double parseTerm() {
        double result = parseFactor();
        skipWhitespace();
        while (pos < expression.size()) {
            char op = expression[pos];
            if (op == '*' || op == '/') {
                ++pos;
                double factor = parseFactor();
                if (op == '*') {
                    result *= factor;
                } else {
                    if (factor == 0.0) {
                        throw std::runtime_error("Division by zero");
                    }
                    result /= factor;
                }
                skipWhitespace();
            } else {
                break;
            }
        }
        return result;
    }

    double parseFactor() {
        skipWhitespace();
        if (pos >= expression.size()) {
            throw std::runtime_error("Unexpected end of expression");
        }

        char ch = expression[pos];
        if (ch == '(') {
            ++pos;
            double result = parseExpression();
            skipWhitespace();
            if (pos >= expression.size() || expression[pos] != ')') {
                throw std::runtime_error("Missing closing parenthesis");
            }
            ++pos;
            return result;
        } else if (std::isdigit(ch) || ch == '.' || ch == '-') {
            return parseNumber();
        } else if (ch == '+') {
            ++pos;
            return parseFactor();
        } else {
            throw std::runtime_error("Unexpected character: " + std::string(1, ch));
        }
    }

    double parseNumber() {
        bool negative = false;
        if (expression[pos] == '-') {
            negative = true;
            ++pos;
        }
        
        size_t startPos = pos;
        bool hasDecimal = false;
        while (pos < expression.size() && (std::isdigit(expression[pos]) || expression[pos] == '.')) {
            if (expression[pos] == '.') {
                if (hasDecimal) {
                    throw std::runtime_error("Invalid number with multiple decimals");
                }
                hasDecimal = true;
            }
            ++pos;
        }
        
        if (startPos == pos && negative) {
            throw std::runtime_error("Incomplete negative number");
        }
        
        std::string numStr = expression.substr(
            negative ? startPos - 1 : startPos, 
            pos - (negative ? startPos - 1 : startPos)
        );
        
        try {
            double value = std::stod(numStr);
            return value;
        } catch (const std::exception&) {
            throw std::runtime_error("Invalid number: " + numStr);
        }
    }
};

int main() {
    std::cout << "=== 表达式计算器 ===" << std::endl;
    std::cout << "支持运算符: + - * / 和括号 ()" << std::endl;
    std::cout << "输入 'exit' 退出程序" << std::endl;
    std::cout << "====================" << std::endl;

    while (true) {
        std::cout << "> ";
        std::string input;
        std::getline(std::cin, input);

        // 处理退出命令
        if (input == "exit" || input == "quit") {
            std::cout << "再见！" << std::endl;
            break;
        }

        // 跳过空输入
        if (input.empty()) {
            continue;
        }

        try {
            Parser parser(input);
            double result = parser.parse();
            std::cout << "结果: " << result << std::endl;
        } catch (const std::exception& e) {
            std::cerr << "错误: " << e.what() << std::endl;
        }
    }

    return 0;
}